package com.example.demo;

import java.io.*;

public class CompareTwoFiles {

    private static int count1 = 0;
    private static int count2 = 0;

    static String arrayLines1[] = new String[countLines("d:\\test.txt")];
    static String arrayLines2[] = new String[countLines("d:\\test.txt")];

    public static void main(String args[]) throws Exception {

//        createFile();
//
        findDifference("d:\\test.txt", "d:\\test2.txt");
        displayRecords();
    }

    public static void createFile() throws Exception {
        BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\test.txt")));
        for (int i = 0; i < 1000000; i++) {
            String str = "763\t0\t1\t132\t西宁测试中学\t83\t\t\t2\t西宁测试中学-修改上报\t上报计划\t\t0\t0\t0\t0\t0\t0\t300\t300\t400\t1000\t0\t1000\t2\t\t\t\t上报计划\t1234567890A\t2022-06-09 19:28:28\t1234567890A\t2022-06-09 19:28:28";
            bfw.write(str + "\r");
        }
        bfw.flush();
        bfw.close();
    }

    public static int countLines(String File) {

        int lineCount = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(File));
            while ((br.readLine()) != null) {
                lineCount++;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lineCount;
    }

    public static void findDifference(String File1, String File2) {
        String contents1 = null;
        String contents2 = null;
        try {
            BufferedReader buf1 = new BufferedReader(new FileReader(File1));
            BufferedReader buf2 = new BufferedReader(new FileReader(File2));

            while ((contents1 = buf1.readLine()) != null) {
                arrayLines1[count1] = contents1;
                count1++;
            }

            while ((contents2 = buf2.readLine()) != null) {
                arrayLines2[count2] = contents2;
                count2++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void displayRecords() {

        for (int i = 0; i < arrayLines1.length && i < arrayLines2.length; i++) {
            String a = arrayLines1[i];
            String b = arrayLines2[i];

            if (!a.contains(b)) {
                System.out.println(a);
            }
        }
    }


}
